package com.fyzk.activity.down_numhistory;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.bean.model.SsqLiShiJX;
import com.tyjc.lottery.R;
import com.fyzk.utils.AppUtil;
import com.fyzk.utils.LogUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class K3HistoryAdapter extends BaseAdapter{

	private List<SsqLiShiJX> liShi;
	private Context context;
	
	private int[] imgs = new int[]{R.drawable.dice_v1_small,R.drawable.dice_v2_small,R.drawable.dice_v3_small,
			R.drawable.dice_v4_small,R.drawable.dice_v5_small,R.drawable.dice_v6_small};
	private List<Bitmap> bitmaps = new ArrayList<Bitmap>();
	
	public K3HistoryAdapter(List<SsqLiShiJX> bellow_lishi, Context context){
		liShi = bellow_lishi;
		this.context = context;
		
		for (int i = 0; i < imgs.length; i++) {
			Bitmap bit = BitmapFactory.decodeResource(context.getResources(), imgs[i]);
			bitmaps.add(bit);
		}
	}

	@Override
	public int getCount() {
		if (liShi == null) {
			return 0;
		}
		return liShi.size();
	}

	@Override
	public Object getItem(int position) {

		return null;
	}

	@Override
	public long getItemId(int position) {

		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		SsqLiShiJX ssqLiShiJX = liShi.get(position);
		
		View view = LayoutInflater.from(context).inflate(R.layout.k3history_listview_layout, null);
		LinearLayout b_ll_list = (LinearLayout)view.findViewById(R.id.k3history_ll);
		b_ll_list.getLayoutParams().height = AppUtil.getHeight()*25/560;
		TextView issue_tv = (TextView)view.findViewById(R.id.k3history_issue);
		TextView prizeNum_tv = (TextView)view.findViewById(R.id.k3history_prizenum);
		TextView andValue_tv = (TextView)view.findViewById(R.id.k3history_andvalue);
		TextView type_tv = (TextView)view.findViewById(R.id.k3history_type);
		ImageView iv1 = (ImageView) view.findViewById(R.id.k3history_prizenum_iv1);
		ImageView iv2 = (ImageView) view.findViewById(R.id.k3history_prizenum_iv2);
		ImageView iv3 = (ImageView) view.findViewById(R.id.k3history_prizenum_iv3);
		
		String issue = ssqLiShiJX.getQi();
		String prizeNum = ssqLiShiJX.getHongQiu();
		List<Integer> prizeIds = getPrizeNum(prizeNum);
		try {
			issue_tv.setText(issue.subSequence(issue.length() - 3, issue.length()));
			prizeNum_tv.setText(prizeIds.get(0) + " " + prizeIds.get(1) + " " + prizeIds.get(2));
			int andValue = prizeIds.get(0) + prizeIds.get(1) + prizeIds.get(2);
			andValue_tv.setText("和值" + andValue);
			type_tv.setText(getType(prizeIds));
			iv1.setImageResource(imgs[prizeIds.get(0)-1]);
			iv2.setImageResource(imgs[prizeIds.get(1)-1]);
			iv3.setImageResource(imgs[prizeIds.get(2)-1]);
		} catch (Exception e) {
			LogUtil.e(e.getMessage());
		}
		
		
		return view;
	}
	
	public void setList(List<SsqLiShiJX> liShi){
		this.liShi = liShi;
		notifyDataSetChanged();
	}
	
	private List<Integer> getPrizeNum(String prizeNum){
		List<Integer> prizeIds = new ArrayList<Integer>();
		if (prizeNum.contains(" ")) {
			String[] p = prizeNum.split(" ");
			for (int i = 0; i < p.length; i++) {
				int pri = 0;
				try {
					pri = Integer.parseInt(p[i]);
				} catch (NumberFormatException e) {
					LogUtil.e(e.getMessage());
				}
				prizeIds.add(pri);
			}
		}
		Collections.sort(prizeIds);
		return prizeIds;
	}
	
	private String getType(List<Integer> prizeIds){
		if (prizeIds.size() != 3) {
			return "";
		}
		String type = "";
		int num1 = prizeIds.get(0);
		int num2 = prizeIds.get(1);
		int num3 = prizeIds.get(2);
		
		if (num1 == num2 && num2 == num3) {
			type = "三同号";
		}else if (num1 == num2 || num1 == num3 || num2 == num3) {
			type = "二同号";
		}else if ((num1 + 1) == num2 && (num2 + 1) == num3) {
			type = "三连号";
		}else{
			type = "三不同号";
		}
		
		return type;
	}
}
